package com.settlement.system.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class StringUtils extends org.springframework.util.StringUtils {

    private static final List<String> allCharacters = Arrays.asList("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz", "0123456789", "!@#");

    /**
     * 替换字符串
     *
     * @param string     原字符串
     * @param preValue   需要替换的字符
     * @param finalValue 替换成的字符
     * @return 替换后的字符串
     */
    public static String replaceStr(String string, String preValue, String finalValue) {
        if (!org.springframework.util.StringUtils.hasText(string)) {
            return null;
        }
        if (!org.springframework.util.StringUtils.hasText(preValue)) {
            return string;
        }
        if (!org.springframework.util.StringUtils.hasText(finalValue)) {
            return string;
        }
        return string.replaceAll(preValue, finalValue);
    }

    public static String randomString(int length) {
        Random random = new Random();
        StringBuilder randomString = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(allCharacters.get(i % 4).length());
            randomString.append(allCharacters.get(i % 4).charAt(randomIndex));
        }
        return randomString.toString();
    }

    public static void main(String[] args) {
        System.out.println(StringUtils.randomString(10));
    }
}
